package com.komon.object.request.bodyIO;

import java.io.IOException;
import java.io.InputStream;

public final class FixedLengthRequestInputStream extends HttpRequestBodyEncodingInputStream {
	private final int content_length;
	
	public FixedLengthRequestInputStream(int contentLength, InputStream socketInputStream){
		super(socketInputStream);
		this.content_length = contentLength;
	}
	
	/* (non-Javadoc)
	 * @see com.komon.object.HttpRequestBodyInputStream#read()
	 */
	@Override
	public int read() throws IOException {
		if (canReadNext()) {
			int result = in.read();
			this.offset.getAndIncrement();
			return result;
		}else {
			return -1;
		}
	}


	protected boolean canReadNext() {
		if (in == null || offset.get() >=  content_length) {
			return false;
		} 
		return true;
	}

	@Override
	public void skipCurrentRequest() throws IOException {
		in.skip(content_length - this.offset.get());
	}
}
